package com.gitee.btssplib.sample

import android.app.Activity
import android.bluetooth.BluetoothDevice
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import com.gitee.btssplib.BTManager
import com.gitee.btssplib.ConnectListener
import com.gitee.btssplib.ReceiveListener
import com.gitee.btssplib.SendListener
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : Activity() {

    var bluetoothDevice: BluetoothDevice? = null

    private val btManager: BTManager = BTManager()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btManager.connectListener = object : ConnectListener {
            override fun onStart() {
                connectStateTextView.text = "正在连接"
            }

            override fun onError(throwable: Throwable) {
                connectStateTextView.text = "连接失败：${throwable.message}"
            }

            override fun onConnected() {
                connectStateTextView.text = "连接成功"
            }
        }

        selectDeviceButton.setOnClickListener {
            startActivityForResult(Intent(this, DevicesActivity::class.java), 1)
        }

        connectButton.setOnClickListener {
            bluetoothDevice?.let {
                btManager.connect(it)
            }
        }
        disconnectButton.setOnClickListener {
            btManager.stop()
            connectStateTextView.text = "已断开连接"
        }

        btManager.receiveListener = object : ReceiveListener {
            override fun onStart() {
            }

            override fun onResult(data: Any) {
                if (dataTextView.text.length > 200) {
                    dataTextView.text = ""
                }
                dataTextView.append("${(data as Int).toByte().toChar()}")
            }

            override fun onError(throwable: Throwable) {
            }
        }

        receiveButton.setOnClickListener {
            btManager.startReceiveData()
        }

        btManager.sendListener = object : SendListener {

            override fun onError(throwable: Throwable) {
            }

            override fun onSuccess(id: Long, data: ByteArray) {
                Toast.makeText(this@MainActivity, "发送${String(data, Charsets.US_ASCII)}成功", Toast.LENGTH_LONG).show()
            }
        }
        sendButton.setOnClickListener {
            btManager.sendData(editText.text.trim().toString().toByteArray(Charsets.US_ASCII))
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        when (requestCode) {
            1 -> {
                if (resultCode == RESULT_OK) {
                    bluetoothDevice = data?.extras?.get("bluetoothDevice") as BluetoothDevice
                    deviceTextView.text = "${bluetoothDevice?.name} / ${bluetoothDevice?.address}"
                }
            }
            else -> {
                super.onActivityResult(requestCode, resultCode, data)
            }
        }
    }
}
